<template>
  <div class="createPost-container">
    <el-form
      ref="postForm"
      :model="postForm"
      label-position="right"
      class="form-container"
    >
      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.status">
        <el-button
          style="margin-left: 10px"
          type="success"
          @click="submit"
        >发布</el-button>
        <el-button style="margin-left: 10px" type="success" @click="back">返回</el-button>

      </sticky>

      <div class="createPost-main-container">
        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="项目名称:"
        >
          <el-input
            v-model="postForm.project_name"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入项目名称"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="开标时间:"
        >
          <el-date-picker
            v-model="postForm.bid_open_time"
            value-format="yyyy-MM-dd"
            type="date"
            class="article-textarea"
            :disabled="true"
            placeholder="选择日期"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="详细地址:"
        >
          <el-input
            v-model="postForm.address_detailed"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入详细地址"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="资格条件:"
        >
          <el-input
            v-model="postForm.qualifications"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入资格条件"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="人员要求:"
        >
          <el-input
            v-model="postForm.researchers"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入人员要求"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="建设单位:"
        >
          <el-input
            v-model="postForm.construction_unit"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入建设单位"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="代理单位:"
        >
          <el-input
            v-model="postForm.agent_unit"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入代理单位"
          />
        </el-form-item>
        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="项目概况:"
        >
          <el-input
            v-model="postForm.project_overview"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入项目概况"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="分值组成:"
        >
          <el-input
            v-model="postForm.scores"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入分值组成"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="资信分情况:"
        >
          <el-input
            v-model="postForm.credit_points"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入资信分情况"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="商务标评分情况:"
        >
          <el-input
            v-model="postForm.business_standard"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            :autosize="{ minRows: 4, maxRows: 10 }"
            placeholder="请输入商务标评分情况"
          />
        </el-form-item>

        <el-divider
          style="margin:10px;"
        ><span
          style="color:#666;font-size:12px;"
        >以上为当前项目审核信息</span></el-divider>
        <el-form
          ref="submitFrom"
          :model="submitFrom"
          label-position="right"
          class="form-container"
        >
          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="业务负责人:"
          >
            <el-input
              v-model="submitFrom.business_director"
              :rows="1"
              type="textarea"
              autosize
              placeholder="不可输入数字"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="业务负责人电话:"
          >
            <el-input
              v-model="submitFrom.director_mobile"
              :rows="1"
              type="textarea"
              autosize
              placeholder="请输入正确的业务负责人电话"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="工程费用:"
          >
            <el-input
              v-model="submitFrom.engineering_cost"
              :rows="1"
              type="textarea"
              autosize
              placeholder="格式:100.00"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="最高限价:"
          >
            <el-input
              v-model="submitFrom.price_ceiling"
              :rows="1"
              type="textarea"
              autosize
              placeholder="格式:100.00"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="中标价:"
          >
            <el-input
              v-model="submitFrom.price_tag"
              :rows="1"
              type="textarea"
              autosize
              placeholder="格式:100.00"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="业务类型:"
          >
            <el-select v-model="submitFrom.business_type" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="投标描述:"
          >
            <el-input
              v-model="submitFrom.tender_describe"
              :rows="1"
              type="textarea"
              autosize
              placeholder="格式:100.00"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="文件上传:"
          >
            <el-upload
              ref="upload"
              class="upload-demo"
              :action="uploadAction"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :before-remove="beforeRemove"
              :on-success="getFilePath"
              multiple
              :limit="5"
              :on-exceed="handleExceed"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
    </el-form>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky' // 粘性header组件
import VDistpicker from 'v-distpicker'
import { getProjectDetail, insertFile, insertTenderInfo } from '@/api/project'
import { getToken } from '@/utils/auth'
import { getInfo } from '@/api/user'

const defaultForm = {
  status: 'draft',
  // projectArea: { province: "广东省", city: "广州市", area: "海珠区" }, //项目区域
  address_detailed: '', // 详细地址
  projectAreaStr: '', // 选择的地区字符串
  bid_open_time: '', // 开标时间
  project_name: '', // 项目名称
  qualifications: '', // 资格条件
  researchers: '', // 人员要求
  constructionUnit: '', // 建设单位
  agentUnit: '', // 代理单位
  projectOverview: '', // 项目概况
  scores: '', // 分值组成
  business_standard: '', // 商务标评分情况
  credit_points: '' // 资信分情况
}
// 需要的表单
const formSubmit = {
  business_director: '', // 业务负责人
  director_mobile: '', // 业务负责人电话
  engineering_cost: '', // 工程费用
  price_ceiling: '', // 最高限价
  price_tag: '', // 中标价
  business_type: '', // 中标价
  tender_describe: '', // 投标描述
  file_path: [] // 上传文件后服务器返回的路片名称
}
// http://dc.tuobao.m.com/storage/images
export default {
  name: 'ArticleDetail',
  components: {
    Sticky,
    VDistpicker
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      id: '', // 当前项目的id
      postForm: Object.assign({}, defaultForm),
      submitFrom: Object.assign({}, formSubmit),
      options: [
        {
          value: '监理',
          label: '监理'
        },
        {
          value: '造价咨询',
          label: '造价咨询'
        },
        {
          value: '代理',
          label: '代理'
        },
        {
          value: '全过程咨询',
          label: '全过程咨询'
        }
      ],
      // 添加头部请求
      headers: {
        Authorization: 'Bearer ' + getToken(),

        post: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      },
      uploadAction: '/adminapi/project/insertFile'
    }
  },
  computed: {},
  created() {
    if (this.isEdit) {
      this.id = this.$route.params && this.$route.params.id
      this.getProjectData(this.id)
    }
    this.getUserInfo()
    this.submitFrom.file_path = []
  },
  methods: {
    // 获取文件返回路径 需要先上传 上传成功后返回路径再保存到数据库
    getFilePath(res) {
      this.submitFrom.file_path.push(res.data)
      // console.log(1);
      // this.$refs.upload.clearFiles();
    },
    // 获取项目
    getProjectData(id) {
      new Promise((reslove, reject) => {
        getProjectDetail({ id: id }).then(response => {
          // console.log(response.data);
          this.postForm = response.data
        })
      })
    },
    back() {
      // console.log( this.submitFrom.file_path);
      this.$router.push('/branch/index')
    },
    // 发布
    submit() {
      new Promise((resolve, reject) => {
        insertTenderInfo({
          id: this.id,
          submitFrom: this.submitFrom,
          username: this.username
        }).then(() => {
          this.$message({
            type: 'success',
            message: '投标信息填写成功!'
          })
          this.$router.push('/branch/index')
        })
      })
    },
    // 文件预览
    handlePreview() {},
    // 文件移除
    handleRemove(file, fileList) {
      // console.log(this.submitFrom.file_path);
      // 删除暂存在需要保存到数据库数组中的数据
      for (let i = 0; i < this.submitFrom.file_path.length; i++) {
        if (this.submitFrom.file_path[i].name == file.name) {
          this.submitFrom.file_path.splice(i, 1)
          return
        }
      }
    },
    // 文件限制
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      )
    },
    // 文件移除前
    beforeRemove(file, fileList) {
      // console.log(this.submitFrom.file_path);
      // console.log(file);
      // console.log(fileList);
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    // 获取当前用户的登录信息
    getUserInfo() {
      new Promise((resolve, reject) => {
        getInfo().then(response => {
          // console.log("info");
          // console.log(response.data);
          this.username = response.data.name // 当前登录用户的姓名
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";
.el-form-item {
  margin: 0px;
}
.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 0px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      text-align: center;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  .el-textarea__inner {
    background-color: white;
    color: #000;
    opacity: 0.7;
  }
  .el-input__inner {
    background-color: white;
    color: #000;
    opacity: 0.7;
  }
}
</style>
